package controller.cartp;

import com.zuche.framework.tradingPlatform.*;
import com.zuche.framework.utils.PropertiesReader;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by PCG on 2016/4/27.
 */
@Controller
public class easyLinkController {

    private String projectAddress;

    public easyLinkController() {
        this.projectAddress = PropertiesReader.getAppointPropertiesAttribute(
                "setting", "projectAddress", String.class);
    }


    /**
     * 易联wap支付
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/easyLinkWapPay.do_")
    public void createNoSms(HttpServletRequest request,
                            HttpServletResponse response) throws IOException {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put(RequestParams.ASYNC_REPLY_URL, "http://10.101.32.9:8083/cartptest/easyLinkPayReturn.do_");
        paramMap.put(RequestParams.SYS_GROUP, request.getParameter("sysGroup"));//账户
        paramMap.put(RequestParams.GROUP_TYPE, "easyLinkWebPay");
        paramMap.put(RequestParams.BUSINESS_MONEY,request.getParameter("money"));
        paramMap.put(RequestParams.CARD_TYPE, request.getParameter("cardType"));
        paramMap.put(RequestParams.CARD_NO, request.getParameter("cardNo"));
        paramMap.put(RequestParams.PAYER_TEL,request.getParameter("mobile"));
        paramMap.put(RequestParams.PAYER_NAME, request.getParameter("name"));
        paramMap.put(RequestParams.IDENTITY_ID, request.getParameter("sfz"));
        paramMap.put(RequestParams.SOURCE_ID, request.getParameter("sourceId"));
        //专车区分是 1:首页绑卡,还是2:充值页面绑卡
        paramMap.put("tp_is_autopay",request.getParameter("autopay"));
        paramMap.put(RequestParams.BUSINESS_ID, ""+System.currentTimeMillis());
        paramMap.put(RequestParams.BUSINESS_NAME, "easyLink");
        paramMap.put(RequestParams.IDENTITY_TYPE,"0");
        paramMap.put(RequestParams.PAYER_ID, request.getParameter("payerId"));
        paramMap.put(RequestParams.IS_VALIDATE, "true");
        TradingServiceExecutor.doMvcBySetting(request, response, paramMap);

    }


    /**
     * 易联绑卡wap支付
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/easyLinkWapBindPay.do_")
    public void createBindPay(HttpServletRequest request,
                            HttpServletResponse response) throws IOException {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put(RequestParams.ASYNC_REPLY_URL, "http://127.0.0.1:8083/cartptest/easyLinkPayReturn.do_");
        paramMap.put(RequestParams.SYS_GROUP, request.getParameter("sysGroup"));//账户
        paramMap.put(RequestParams.GROUP_TYPE, "easyLinkWebPay");
        //绑卡id
        paramMap.put(RequestParams.BIND_ID, request.getParameter("bindId"));
        paramMap.put(RequestParams.BUSINESS_MONEY,request.getParameter("money"));
        paramMap.put(RequestParams.BUSINESS_ID, ""+System.currentTimeMillis());
        paramMap.put(RequestParams.BUSINESS_NAME, "easyLink");
        paramMap.put(RequestParams.PAYER_ID, request.getParameter("payerId"));
        TradingServiceExecutor.doMvcBySetting(request, response, paramMap);

    }

    /**
     * 易联短信+H5支付
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/easyLinkSmsH5Pay.do_")
    @ResponseBody
    public Object easyLinkSmsH5Pay(HttpServletRequest request,
                            HttpServletResponse response) throws IOException {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put(RequestParams.ASYNC_REPLY_URL, "http://10.101.32.9:8083/cartptest/easyLinkPayReturn.do_");
        paramMap.put(RequestParams.SYS_GROUP, request.getParameter("sysGroup"));//账户
        paramMap.put(RequestParams.GROUP_TYPE, "easyLinkSmsH5");
        paramMap.put(RequestParams.BUSINESS_MONEY,request.getParameter("money"));
//        paramMap.put(RequestParams.CARD_TYPE, request.getParameter("cardType"));
        paramMap.put(RequestParams.CARD_NO, request.getParameter("cardNo"));
        paramMap.put(RequestParams.PAYER_TEL,request.getParameter("mobile"));
        paramMap.put(RequestParams.PAYER_NAME, request.getParameter("name"));
        paramMap.put(RequestParams.IDENTITY_ID, request.getParameter("sfz"));
//        paramMap.put(RequestParams.SOURCE_ID, request.getParameter("sourceId"));
        //专车区分是 1:首页绑卡,还是2:充值页面绑卡
//        paramMap.put("tp_is_autopay",request.getParameter("autopay"));
        paramMap.put(RequestParams.BUSINESS_ID, ""+System.currentTimeMillis());
        paramMap.put(RequestParams.BUSINESS_NAME, "easyLink");
        paramMap.put(RequestParams.IDENTITY_TYPE,"0");
        paramMap.put(RequestParams.PAYER_ID, request.getParameter("payerId"));
//        paramMap.put(RequestParams.IS_VALIDATE, "true");
        return TradingServiceExecutor.doRpc(paramMap,true);

    }

    /**
     * 易联SDK 支付绑卡
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/easyLinkSdkPay.do_")
    @ResponseBody
    public Object createSdk(HttpServletRequest request,
                            HttpServletResponse response) throws IOException {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put(RequestParams.ASYNC_REPLY_URL, "http://10.101.32.9:8083/cartptest/easyLinkPayReturn.do_");
        paramMap.put(RequestParams.SYS_GROUP, request.getParameter("sysGroup"));//账户
        paramMap.put(RequestParams.GROUP_TYPE, "easyLinkSdkPass");
        paramMap.put(RequestParams.BUSINESS_MONEY,request.getParameter("money"));
        paramMap.put(RequestParams.CARD_TYPE, request.getParameter("cardType"));
        paramMap.put(RequestParams.CARD_NO, request.getParameter("cardNo"));
        paramMap.put(RequestParams.PAYER_TEL,request.getParameter("mobile"));
        paramMap.put(RequestParams.PAYER_NAME, request.getParameter("name"));
        paramMap.put(RequestParams.IDENTITY_ID, request.getParameter("sfz"));
        paramMap.put(RequestParams.SOURCE_ID, request.getParameter("sourceId"));
        //专车区分是 1:首页绑卡,还是2:充值页面绑卡
        paramMap.put("tp_is_autopay",request.getParameter("autopay"));
        paramMap.put(RequestParams.BUSINESS_ID, ""+System.currentTimeMillis());
        paramMap.put(RequestParams.BUSINESS_NAME, "easyLink");
        paramMap.put(RequestParams.IDENTITY_TYPE,"0");
        paramMap.put(RequestParams.PAYER_ID, request.getParameter("payerId"));
        paramMap.put(RequestParams.IS_VALIDATE, "true");
        return TradingServiceExecutor.doRpc(paramMap, true);
    }


    /**
     * 易联绑卡后 sdk支付
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/easyLinkSdkBindPay.do_")
    @ResponseBody
    public Object createSdkBindPay(HttpServletRequest request,
                              HttpServletResponse response) throws IOException {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put(RequestParams.ASYNC_REPLY_URL, "http://127.0.0.1:8083/cartptest/easyLinkPayReturn.do_");
        paramMap.put(RequestParams.SYS_GROUP, request.getParameter("sysGroup"));//账户
        paramMap.put(RequestParams.GROUP_TYPE, "easyLinkSdkPass");
        //绑卡id
        paramMap.put(RequestParams.BIND_ID, request.getParameter("bindId"));
        paramMap.put(RequestParams.BUSINESS_MONEY,request.getParameter("money"));
        paramMap.put(RequestParams.BUSINESS_ID, ""+System.currentTimeMillis());
        paramMap.put(RequestParams.BUSINESS_NAME, "easyLink");
        paramMap.put(RequestParams.PAYER_ID, request.getParameter("payerId"));
        return TradingServiceExecutor.doRpc(paramMap,true);

    }


    /**
     * 易联卡信息查询
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/easyLinkQueryCard.do_")
    @ResponseBody
    public Object easyLinkQueryCard(HttpServletRequest request,
                                   HttpServletResponse response) throws IOException {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put(RequestParams.SYS_GROUP, request.getParameter("sysGroup"));//账户
        paramMap.put(RequestParams.GROUP_TYPE, "easyLinkQueryCard");
        paramMap.put(RequestParams.CARD_NO, request.getParameter("cardNo"));
        paramMap.put(RequestParams.BUSINESS_ID, ""+System.currentTimeMillis());
        paramMap.put(RequestParams.BUSINESS_NAME, "easyLink");
        return TradingServiceExecutor.doRpc(paramMap,true);

    }


    @RequestMapping("/easyLinkPayReturn.do_")
    public void easyLinkPayReturn(HttpServletRequest request,HttpServletResponse response){

        String status = request.getParameter(ReplyRequestParams.STATUS);
        if(Status.SUCCESS.equals(status)){
            //开始回调处理
            //获得请求时发起的自定义参数
            String selfParam = request.getParameter(RequestParams.CUSTOMIZED_FLAG
                    +"selfParam");
            System.out.println(selfParam);
            try{
                OutputStream output = response.getOutputStream();
                output.write("http://127.0.0.1:8083/cartptest/success.jsp".getBytes());
            }catch (Exception e){
                e.printStackTrace();
            }
            //加入固定标记，通知交易平台回调成功
            response.addHeader("CAR_TP", Boolean.TRUE.toString());
            System.out.println("支付成功回调！");
        }else{
            System.out.println("支付失败回调！");
        }
    }

    @RequestMapping("/easyLinkHandleBankId.do_")
    public void handlerBankSourceId(){
        String filePath = "E:\\文档\\交易接口文档\\易联支付\\易联支持银行简称对应快钱\\支持银行简称.txt";
        try {
            FileReader file = new FileReader(filePath);
            BufferedReader reader = new BufferedReader(file);
            String input;
            int i =1;
            while((input=reader.readLine())!=null) {
                if(input.indexOf("CCB") == -1 && input.indexOf("CITIC") == -1 && input.indexOf("CMBC") == -1
                        && input.indexOf("ICBC") == -1 && input.indexOf("CMB") == -1 && input.indexOf("ABC") == -1
                        && input.indexOf("POST") == -1 && input.indexOf("SPDB") == -1 && input.indexOf("BOC") == -1
                        && input.indexOf("CEB") == -1 && input.indexOf("CIB") == -1 && input.indexOf("HXB") == -1
                        && input.indexOf("BCOM") == -1 && input.indexOf("BOB") == -1 && input.indexOf("PAB") == -1
                        && input.indexOf("GDB") == -1
                        && input.indexOf("visaVISA") == -1 && input.indexOf("ALL") == -1
                        && input.indexOf("CSB") == -1 && input.indexOf("LZB") == -1 && input.indexOf("SHB") == -1
                        && input.indexOf("HLJRCC") == -1 && input.indexOf("JSRCC") == -1 && input.indexOf("SHB") == -1){
                    if(input.indexOf("#####") > -1){
                        System.out.println(input.substring(input.indexOf("######")));
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


}
